package com.jww.service.impl;

import com.github.pagehelper.PageHelper;
import com.jww.domain.store.Course;
import com.jww.mapper.store.CourseMapper;
import com.jww.service.CourseService;
import com.jww.utils.MapperUtil;
import com.jww.utils.MyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class CourseServiceImpl implements CourseService {

    @Override
    public void save(Course course) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        //主键id处理
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        course.setId(id);

        mapper.save(course);
        MyUtils.close(sqlSession);
    }

    @Override
    public List<Course> findAll(String pageNum, String pageSize) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyUtils.getSqlSession();
            CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
            //分页
            //默认当前页为1
            int pageN = 1;
            //默认每页显示条目数为10
            int pageS = 10;
            //对获取的当前页码和每页显示条目数进行类型处理

            if (StringUtils.isNotBlank(pageNum)) {
                pageN = Integer.parseInt(pageNum);
            }

            if (StringUtils.isNotBlank(pageSize)) {
                pageS = Integer.parseInt(pageSize);
            }
            //设置分页参数：当前页码 每页显示的条目数
            PageHelper.startPage(pageN, pageS);

            List<Course> list = mapper.findAll();
            return list;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询失败====>>");
        } finally {
            MyUtils.close(sqlSession);
        }

    }

    @Override
    public boolean deleteByIds(String ids) {

        SqlSession sqlSession = null;
        boolean flag = false;
        try {
            sqlSession = MyUtils.getSqlSession();
            CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

            String[] id = ids.split(",");
            mapper.delete(id);
            flag = true;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyUtils.close(sqlSession);
        }
        return flag;
    }

    @Override
    public Course findById(String id) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);


        Course course = mapper.findById(id);
        MyUtils.close(sqlSession);
        return course;
    }

    @Override
    public void update(Course course) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);

        mapper.update(course);
        MyUtils.close(sqlSession);
    }

    //    下拉框的list方法
    @Override
    public List<Course> list() {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        List<Course> list = mapper.findAll();
        MapperUtil.close(session);
        return list;

    }
}
